//
//  GuessViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "GuessViewController.h"

@interface GuessViewController ()

@end

@implementation GuessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = @"猜你喜欢";
    self.navigationItem.titleView = self.TitleView;
    
    _dataArr = [[NSMutableArray alloc] init];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    _GuessView = [[NormalView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _GuessView.tableView.dataSource = self;
    _GuessView.tableView.delegate = self;
    _GuessView.tableView.backgroundColor = [UIColor clearColor];
    [_GuessView getLocalImage:@"背景3.jpg"];
    [self.view addSubview:_GuessView];
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    

    
    [self getDataForGuess];
    
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];
    
    Legacy = [SinglePlayer shareHandleSinglePlayer];
    
    
    [_GuessView release];
    
    // Do any additional setup after loading the view.
}



- (void)getDataForGuess
{
    [AFN urlString:GuessYouLike getAFNdata:^(id responseObject) {
        
        NSArray *dataArr = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            ReDetailModel *model = [[ReDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArr addObject:model];
            [model release];
        }
        HUD.hidden = YES;
        [_GuessView.tableView reloadData];
    }];
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    
    ReDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == NO) {
        
        cell = [[ReDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    if (_dataArr.count > indexPath.row) {
        ReDetailModel *model = [_dataArr objectAtIndex:indexPath.row];
        [cell getDataForCell:model];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 80*ADJUSTWIDTH;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReDetailModel *reDetailmodel = [_dataArr objectAtIndex:indexPath.row];
    BarPlayer.reDetailModel = reDetailmodel;
    BarPlayer.dataArr = _dataArr;
    BarPlayer.index = indexPath.row;
    [self playeMusicWithModel:reDetailmodel index:indexPath.row];
    
    
}


// 播放方法
- (void)playeMusicWithModel:(ReDetailModel *)model index:(NSInteger)index
{
    if (model.audition_list.count > 1) {
        NSDictionary *dic = [model.audition_list objectAtIndex:1];
        NSString *url_string = [dic objectForKey:@"url"];
        [Legacy play:url_string];
        
    } else {
        NSDictionary *dic = [model.audition_list objectAtIndex:0];
        NSString *url_string = [dic objectForKey:@"url"];
        [Legacy play:url_string];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
